package lxy.contacttest;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import lxy.contacttest.utils.ContactBean;

/**
 * Created by dengdayi
 * Date: 2016/06/22 10：46
 * 联系人列表适配器
 */
public class ContactsAdapter extends BaseAdapter {
    private Context context;
    private List<ContactBean> listContact = new ArrayList<>();

    public ContactsAdapter(Context context) {
        this.context = context;
    }

    public void clear() {
        listContact.clear();
        notifyDataSetChanged();
    }

    public void addAllItem(List<ContactBean> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        listContact.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listContact.size();
    }

    @Override
    public ContactBean getItem(int position) {
        return listContact.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.list_item, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ContactBean con = listContact.get(position);

        holder.name.setText(con.getName());
        return convertView;
    }

    static class ViewHolder {
        TextView name;
    }
}
